package com.google.code.b0rx0r.ui;

/*
 *	SimpleWaveformDisplay.java
 */

/*
 * Copyright (c) 2003 by Matthias Pfisterer
 * Copyright (c) 2003 by Florian Bomers
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
|<---            this code is formatted to fit into 80 columns             --->|
*/


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import org.tritonus.share.sampled.FloatSampleBuffer;

/**
 */
public class SimpleWaveformDisplay
extends JPanel
{
	private float[][]	m_anDisplayData;
	private int	m_nWidth;

	public static void calculate(FloatSampleBuffer fsb, float[][] buffer, int start, int length) {
		int width = buffer[0].length;
		
		if (length > 0) {
			int nSamplesPerPixel = length / width;
			for (int c=0; c<fsb.getChannelCount(); c++) {
				float data[] = fsb.getChannel(c);
				for (int i = 0; i < width; i++)
				{
					float value = 0;
					for (int j = 0; j < nSamplesPerPixel; j++)
					{
						value += Math.abs(data[start + i * nSamplesPerPixel + j]);
					}
					value /= nSamplesPerPixel;
					buffer[c][i] = value;
				}
			}
		} else {
			for (int c=0; c<fsb.getChannelCount(); c++) {
			  for (int i = 0; i < width; i++) {
				  buffer[c][i] = 0.0f;
			  }
			}
		}
	}

	public static void calculate(FloatSampleBuffer fsb, float[][] buffer) {
		int width = buffer.length;
		
		int length = 0;
		if (width > 0 && fsb != null) {
			if (fsb.getChannelCount() > 0) {
				length = fsb.getSampleCount();
			}
		}

		calculate(fsb, buffer, 0, length);
		
	}

	public static float[][] calculate(FloatSampleBuffer fsb, int width, int height) {
		float[][] retVal = new float[fsb.getChannelCount()][width];
		calculate(fsb, retVal);
		return retVal;
	}

	public SimpleWaveformDisplay(int nWidth, int nHeight)
	{
		m_nWidth = nWidth;
		Dimension dim = new Dimension(nWidth, nHeight);
		setMinimumSize(dim);
		setMaximumSize(dim);
		setPreferredSize(dim);
		m_anDisplayData = new float[1][nWidth];
//		setForeground(Color.GREEN);
//		setBackground(Color.BLACK);
	}





	

	public void setData(FloatSampleBuffer fsb, int start, int length)
	{
		m_anDisplayData = new float[fsb.getChannelCount()][m_nWidth];

				calculate(fsb, m_anDisplayData, start, length);

		repaint();
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		int nHeight = getHeight() / m_anDisplayData.length;
		
		for (int c = 0; c<m_anDisplayData.length; c++) {
		  for (int i = 0; i < m_nWidth; i++) {
				int value = (int) (m_anDisplayData[c][i] * nHeight);
				int yOffset = nHeight * c;
				int y1 = yOffset + ((nHeight - 2 * value) / 2);
				int y2 = y1 + 2 * value;
				g.drawLine(i, y1, i, y2);
		  }
		}
	}

	public void setWidth(int width) {
		m_nWidth = width;
	}



}



/*** SimpleWaveformDisplay.java ***/

